package org.origin.centre.filter;

import jakarta.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * HeaderMapRequestWrapper
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
public class HeaderMapRequestWrapper extends XssHttpServletRequest {
    protected Map<String, String> headerMap = new HashMap<>();

    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public void addHeader(String name, String value) {
        this.headerMap.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (this.headerMap.containsKey(name)) {
            headerValue = this.headerMap.get(name);
        }
        return headerValue;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        ArrayList<String> names = Collections.list(super.getHeaderNames());
        names.addAll(this.headerMap.keySet());
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        ArrayList<String> names = Collections.list(super.getHeaders(name));
        if ((this.headerMap.containsKey(name))) {
            names.add(this.headerMap.get(name));
        }
        return Collections.enumeration(names);
    }
}
